package com.wodwl.dao;

import java.io.IOException;

import java.io.Reader;

import java.util.List;

import com.ibatis.common.resources.Resources;

import com.ibatis.sqlmap.client.SqlMapClient;

import com.ibatis.sqlmap.client.SqlMapClientBuilder;
import com.wodwl.entity.Bbs;

public class BbsDao {

	public static enum Mode {

		INSERT, UPDATE, DELETE

	}
	public void update(Object arg, Mode mode) {

		SqlMapClient smc = null;

		try {

			smc = getSqlMapClient();
			smc.startTransaction();

			switch (mode) {

			case INSERT:
				smc.insert("bbs.insertBbs", arg);
				break;

			case UPDATE:
				smc.update("bbs.updateBbsByBid", arg);
				break;

			case DELETE:
				smc.delete("bbs.deleteBbs", arg);
				break;

			}

			smc.commitTransaction();

		} catch (Exception e) {

			e.printStackTrace();

		} finally {
			endTransaction(smc);
		}
	}
	public List<Bbs> getBbssByUsername(String username) {
		SqlMapClient smc = null;
		List<Bbs> Bbss=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Bbss =(List<Bbs>) smc.queryForList("bbs.getBbssByUsername", username);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Bbss;

	}
	public List<Bbs> getAllBbss() {
		SqlMapClient smc = null;
		List<Bbs> Bbss=null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Bbss =(List<Bbs>) smc.queryForList("bbs.getAllBbss", null);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);
		}
		return Bbss;

	}

	public Bbs getBbsByBid(int id) {
		Bbs Bbs = null;
		SqlMapClient smc = null;
		try {
			smc = getSqlMapClient();
			smc.startTransaction();
			Bbs = (Bbs) smc.queryForObject("bbs.getBbsByBid", id);
			smc.commitTransaction();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			endTransaction(smc);

		}
		return Bbs;

	}

	// //
	// ////////////////////////////////////////////////////////////////////////

	private SqlMapClient getSqlMapClient() throws IOException {

		// 初始化ibatis, 获得一个SqlMapClient对象

		String resource = "ibatis/SqlMapConfig.xml";
		Reader reader = Resources.getResourceAsReader(resource);
		return SqlMapClientBuilder.buildSqlMapClient(reader);

	}

	private void endTransaction(SqlMapClient smc) {

		if (smc == null) {
			return;
		}
		try {
			smc.endTransaction();
		} catch (Exception e2) {
			e2.printStackTrace();
		}

	}

	public static void main(String[] args) {
		BbsDao dao = new BbsDao();
		Bbs bbs=new Bbs();
		
		
		 //增
		bbs.setTitle("insert");
		bbs.setContent("hello world");
		dao.update(bbs, Mode.INSERT);
		
		
		
		//查
		bbs=dao.getBbsByBid(7);
		System.out.println(bbs.getTitle());
		
		
		//改
		bbs.setBid(7);
		bbs.setTitle("update");
		dao.update(bbs, Mode.UPDATE);
		
		
		
		dao.update(8, Mode.DELETE);
		

		List list=dao.getAllBbss();
		//List<Bbs> list=dao.getBbssByUsername("aaa");
		 System.out.println(list.size());
	}

}